﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NetTools;


namespace wd_10_local_ipv4_v1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_ipv4(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(GetLocalIPv4());
        }
        public static string GetLocalIPv4()
        {
            string ip = "";
            try
            {
                string HostName = Dns.GetHostName();
                //ip = HostName;

                IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
                for(int i=0; i<IpEntry.AddressList.Length; i++)
                {
                    if(IpEntry.AddressList[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        //ip = IpEntry.AddressList[i].ToString();   84.1?
                        return IpEntry.AddressList[i].ToString();   // 正确用法
                    }
                    else
                    {

                    }
                }

                return ip;
            }
            catch(Exception ex)
            {
                return "null"+ex.Message;
            }
        }

        private void Button_Click_Is_In_List(object sender, RoutedEventArgs e)
        {
            var rangeA = IPAddressRange.Parse("192.168.0.0/255.255.255.0");
            bool b1 = rangeA.Contains(IPAddress.Parse("192.168.0.34"));
            if (b1)
                MessageBox.Show("ok");
            else
                MessageBox.Show("false"); ;

        }
    }
}
